/**
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
*
* PmPurchaseContractService
* 外包合同申请
*
* 修改纪录
* 2020-05-12 版本：1.0 shenyouyi 创建。
* @version 版本：1.0
* @author 作者：shenyouyi
* 创建日期：2020-05-12
*/
package com.topscomm.pm.service.impl;

import com.topscomm.pm.pojo.PmPurchaseContractEntity;
import com.topscomm.pm.service.IPmProjectService;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.pm.service.IPmPurchaseContractService;
import com.topscomm.pm.service.impl.auto.PmPurchaseContractServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "pmPurchaseContractService")
@Scope(value = "singleton")
public class PmPurchaseContractService extends PmPurchaseContractServiceAuto implements IPmPurchaseContractService {

	@Autowired
	private IPmProjectService pmProjectService;

	/**
	 * @author: shenyouyi
	 * @date: 2020-05-12
	 * @param entity
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(PmPurchaseContractEntity.FieldDocno, docNo);
		}
	}

	/**
	  * @author: shenyouyi
	  * @date: 2020/6/16
	  * @param
	  * @description:
	  **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	protected void afterDelApprove(TapEntity entity, SessionUserBean sessionUserBean) {
		// 查询出当前合同，已经审批通过的合同金额总和
		Long id = entity.getId();
		this.updatePurchasecontractmoneyOfProject(id.toString(), false);
		super.afterDelApprove(entity, sessionUserBean);
	}

	/**
	  * @author: shenyouyi
	  * @date: 2020/6/16
	  * @param
	  * @description: 采购合同申请审批通过或者取消审批后执行的方法
	  **/
	@Override
	public void updatePurchasecontractmoneyOfProject(String sourceId, boolean approvalSuccess) {
		PmPurchaseContractEntity pmPurchaseContractEntity = this.queryById(sourceId);
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectid = {0} and state = {1} and id <> {2}", pmPurchaseContractEntity.getProjectid(), TapSystemConst.TapApprovalBillStateEnum.Approved, sourceId);
		BigDecimal totalMoney = BigDecimal.ZERO;
		if(approvalSuccess){
			totalMoney = pmPurchaseContractEntity.getContractmoney();
		}
		List<String> queryFields = new ArrayList<>();
//		queryFields.add("sum(contractmoney) as totalmoney");
		queryFields.add("sum(NVL(contractmoney,0)) as totalmoney");
		List<Map<String,Object>> contractMoneyList = this.queryMapFieldsByWhere(sql.toString(), queryFields);
		if(contractMoneyList.size()>0 && contractMoneyList.get(0)!=null){
			BigDecimal partMoney =  (BigDecimal) contractMoneyList.get(0).get("totalmoney");
			totalMoney = totalMoney.add(partMoney);
		}
		// 更新项目表中的采购合同金额
		Map<String,Object> updateFields = new HashMap<>();
		updateFields.put("purchasecontractmoney", totalMoney);
		sql.clear();
		sql.appendSingle("id = {0}", pmPurchaseContractEntity.getProjectid());
		pmProjectService.updateByWhere(updateFields, sql.toString());
	}
}
